<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<style>
			html,
			body {
				width: 100%;
				height: 100%;
			}
		</style>
	</head>

	<body>

		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style=""></a>
			<div class="mui-input-row mui-search mui-pull-right" style="position: absolute;margin-left: 10%;width: 85%;">
				<input id="e_key" type="search" class="mui-input-clear" placeholder="请输入要搜索的公司">
			</div>
		</header>

		<div class="mui-content">
			<div class="mui-content" style="position: absolute;padding-top: 0px;width: 100%;height: 100%;">
				<ul id="listView" class="mui-table-view" style="margin-top: 0px;width: 100%;height: 100%;">
				</ul>
			</div>
		</div>
		<script src="../js/mui.min.js"></script>
		<script src="../js/mui.lazyload.js"></script>
		<script src="../js/mui.lazyload.img.js"></script>
		<script src="../js/initData.js"></script>
		<script type="text/javascript">
			mui.init();

			function getCompanyByQ(city, zaiti) {
				list.innerHTML = "";
				body = [];
				loadData("http://api.map.baidu.com/geosearch/v3/local", {
					"ak": "IbRZThgkSbo11nTpx3Ue4Olvym059LpR",
					"geotable_id": "147704",
					"region": city,
					"page_size": "50",
					"q": zaiti,
				})
			}
			var body = [];

			function loadData(url, getdata) {
				console.log("url=" + url + "  data=" + getdata);
				plus.nativeUI.showWaiting("数据加载中。。");
				mui.ajax(url, {
					data: getdata,
					dataType: "json",
					type: 'get', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒；
					success: function(data) {
						plus.nativeUI.closeWaiting();
						console.log(JSON.stringify(data))
						body = data.contents;
						addDataItem(body);
						console.log("公司" + JSON.stringify(data));
					},
					error: function(xhr, type, errorThrown) {
						//异常处理；
						console.log(type);
						console.log(errorThrown);
						plus.nativeUI.closeWaiting();
					}
				});
			};

			function addTags(tags) {
				var value = "";
				if(tags != null) {
					for(var i = 0; i < tags.split(",").length; i++) {
						var tag = '<p style="border:1px solid #CF2D28;display: inline;color: #CF2D28;font-size: 50%;">' + tagsToChinese(tags.split(",")[i]) + '</p>&nbsp;';
						value = value + tag;
					}
				} else {
					value = "</p>&nbsp;";
				}
				return value;
			}

			function addStar(num) {
				var _star = '<span class="mui-icon mui-icon-star-filled" style="font-size: 100%;"></span>';
				var _unstar = '<span class="mui-icon mui-icon-star" style="font-size: 100%;"></span>';
				var value = "";
				for(var i = 0; i < 5; i++) {
					if(i < num) {
						value = value + _star;
					} else {
						value = value + _unstar;
					}
				}
				return value;
			}
			var list = document.getElementById("listView");

			function addDataItem(body) {
				for(i = 0; i < body.length; i++) {
					var li = document.createElement("li");
					li.className = "mui-table-view-cell mui-media";
					var company_descr = body[i].company_descr;
					if(company_descr == null) {
						company_descr = "暂无简介";
					};
					var company_brand = body[i].brand;
					if(company_brand == null) {
						company_brand = "<br />";
					};
					var company_title_image = "../image/loading.jpg";
					if(body[i].images != null) {
						var imageStr = body[i].images;
						if(imageStr.indexOf(",") == 0) {
							imageStr = imageStr.substring(1);
						}
						var imageArray = imageStr.split(",");
						console.log(imageArray.length)

						if(imageArray.length > 0) {
							company_title_image = "http://upday.ecst.com.cn:81/" + imageArray[0];
							console.log(company_title_image)
						}
					};
					li.innerHTML = '<a style="height: 100%;"><img class="mui-pull-left" data-lazyload=' + company_title_image + ' style="width: 100px; height: 100px;padding-right: 2%;" /><div class="mui-media-large" style="width: 100%; height: 20%;"><p class="mui-ellipsis" style="color: #000000;">' + body[i].company_name + '</p><p class="mui-ellipsis" style="color: #000000;">' + company_brand + '</p>' + addStar(body[i].score) + '<br />' + addTags(body[i].tags_) + '<br /><p class="mui-ellipsis-2" style="font-size: 80%;">' + company_descr + '</p></div></a>';
					li.setAttribute("com_area", body[i].company_area);
					li.setAttribute("com_id", body[i].company_id);
					li.setAttribute("com_num", body[i].companyNum);
					li.setAttribute("com_phone", body[i].phone);
					li.setAttribute("com_name", body[i].company_name);
					li.setAttribute("com_address", body[i].address);
					li.setAttribute("com_type", body[i].company_type);
					li.setAttribute("com_location_jingdu", body[i].location[0])
					li.setAttribute("com_location_weidu", body[i].location[1])
					li.setAttribute("com_images", body[i].images)
					li.setAttribute("com_score", body[i].score)
					li.setAttribute("com_level", body[i].level)
					li.setAttribute("com_tags", body[i].tags_)
					console.log("" + i + body[i].company_type);
					li.addEventListener("tap", function() {
						var com_area = this.getAttribute("com_area");
						var com_num = this.getAttribute("com_num");
						var com_phone = this.getAttribute("com_phone");
						var com_name = this.getAttribute("com_name");
						var com_address = this.getAttribute("com_address");
						var com_type = this.getAttribute("com_type");
						var com_location_jingdu = this.getAttribute("com_location_jingdu");
						var com_location_weidu = this.getAttribute("com_location_weidu")
						var com_descr = this.getAttribute("com_descr")
						var com_score = this.getAttribute("com_score")
						var com_level = this.getAttribute("com_level")
						var com_images = this.getAttribute("com_images")
						var com_id = this.getAttribute("com_id")
						var com_tags = this.getAttribute("com_tags")
						plus.nativeUI.showWaiting("数据加载中。。");
						plus.webview.create("search_show.html", "", "", {
							company_area: com_area,
							companyNum: com_num,
							phone: com_phone,
							company_name: com_name,
							company_type: com_type,
							address: com_address,
							location_jingdu: com_location_jingdu,
							location_weidu: com_location_weidu,
							company_descr: com_descr,
							score: com_score,
							level: com_level,
							images: com_images,
							company_id: com_id,
							company_tags: com_tags,
						});
					});
					list.appendChild(li);
				};
				mui(".mui-table-view-cell").imageLazyload({
					placeholder: '../image/loading.jpg'
				});
			}
			mui.plusReady(function() {
				var e_key = document.getElementById("e_key");
				e_key.addEventListener("keyup", function(e) {
					if(e.keyCode == 13) {
						getCompanyByQ("上海市", e_key.value);
					}
				})
			})
		</script>
	</body>

</html>